/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.comparator;

import org.jfab.core.model.CGAnnotation;
import org.jfab.core.model.CGType;

/**
 * Provides a code generator annotation comparator.
 */
public class CGAnnotationComparator implements CGComparator<CGAnnotation>
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    @Override
    public int compare(final CGAnnotation object1, final CGAnnotation object2)
    {
        int answer = -1;

        if (object1 == object2)
        {
            answer = 0;
        }
        else if (object1 == null)
        {
            answer = 1;
        }
        else if (object2 == null)
        {
            answer = -1;
        }
        else
        {
            final CGType type1 = object1.getType();
            final CGType type2 = object2.getType();

            if (type1 == type2)
            {
                answer = 0;
            }
            else if (type1 == null)
            {
                answer = 1;
            }
            else if (type2 == null)
            {
                answer = -1;
            }
            else
            {
                final String name1 = type1.getName();
                final String name2 = type2.getName();
                answer = name1.compareTo(name2);
            }
        }

        return answer;
    }
}
